<?php
/**
 * Date: 2024-09-11
 */

namespace module\web\controller;

use app\library\ImgCaptcha;
use app\model\Module;
use mof\ApiController;
use mof\ApiResponse;
use mof\utils\Arr;
use think\response\Json;

class Index extends ApiController
{
    public function config(): Json
    {
        //获取可用模块
        $modules = array_map(fn($m) => $m['name'], Module::enabledModules());

        //获取配置信息
        $webConfigs = [];
        foreach ($modules as $name) {
            $cacheKey = "{$name}_config";
            if ($moduleConfig = app('cache')->get($cacheKey)) {
                $_config = [];
                foreach ($moduleConfig as $item) {
                    //($item['extra']['client_cfg'] ?? 0) && $webConfigs[$name][$item['name']] = $item['value'];
                    if ($item['extra']['client_cfg'] ?? 0) {
                        $_config[$item['name']] = $item['value'];
                    }
                }
                if ($_config) {
                    $_config = Arr::coverToMultidimensional($_config);
                    $webConfigs[$name] = $_config;
                }
            }
        }

        return ApiResponse::success($webConfigs);
    }

    public function captcha(): Json
    {
        $data = ImgCaptcha::create();
        return ApiResponse::success(['image' => $data['image']]);
    }
}